﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BDGetQuote.Utilities;

namespace BDGetQuote.Web.Models
{
    public class UserInfoRepository: IUserInfoRepository
    {
        public bool CheckLogin(string username, string password, out string message)
        {
            using (var _dbcontext=new BDQuoteDBDataContext())
            {
                message = string.Empty;
                var result = false;
                var _user = _dbcontext.BO_UserInfos.FirstOrDefault(x => x.UserId == username && !x.IsDeleted);
                if (_user != null)
                {
                    var _maxWrongPasswordAtempts = int.Parse(Utils.GetAppSettingByKey(Constants.MaxWrongPasswordAtempts));
                    var _waitTimeForWrongPasswordAtempts = int.Parse(Utils.GetAppSettingByKey(Constants.WaitTimeForWrongPasswordAtempts));
                    if (_user.WrongLoginAttempt > _maxWrongPasswordAtempts && _user.LastWrongLoginDate.HasValue && _user.LastWrongLoginDate.Value.AddMinutes(_waitTimeForWrongPasswordAtempts) > DateTime.Now)
                    {
                        message = "You inputed wrong password more than " + _maxWrongPasswordAtempts + " times continuously. Please try again after " + _waitTimeForWrongPasswordAtempts + " minutes";
                    }
                    else if (_user.Password != password)
                    {
                        message = "Password is not correct.Please try again";
                        _user.WrongLoginAttempt++;
                        _user.LastWrongLoginDate = DateTime.Now;
                    }
                    else 
                    {
                        if (_user.Active)
                        {
                            if (_user.IsLocked)
                            {
                                message = "Your account is locked. Please contact to Administrator";
                            }
                            else
                            {
                                _user.WrongLoginAttempt = 0;
                                _user.LastLoginDate = DateTime.Now;
                                result = true;
                            }
                        }
                        else
                        {
                            message = "Your account is not activated. Please contact to Administrator";
                        }
                    }
                    _dbcontext.SubmitChanges();
                }
                else
                {
                    message = "Username is not correct. Please try again";
                }
                return result;
            }
        }
        /// <summary>
        /// Get user info exclude password
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserInfo GetByUserId(string userId)
        {
            using (var _dbcontext= new BDQuoteDBDataContext())
            {
                 var _user = _dbcontext.BO_UserInfos.FirstOrDefault(x => x.UserId == userId && !x.IsDeleted);
                 if (_user != null)
                 {
                     return new UserInfo()
                     {
                         UserId = _user.UserId,
                         Active = _user.Active,
                         Address1 = _user.Address1,
                         Address2 = _user.Address2,
                         City = _user.City,
                         Company = _user.Company,
                         Country = _user.Country,
                         CreatedDate = _user.CreatedDate,
                         Email = _user.Email,
                         Fax = _user.Fax,
                         FirstName = _user.FirstName,
                         IsAdmin = _user.IsAdmin,
                         IsLocked = _user.IsLocked,
                         IsSystemAccount = _user.IsSystemAccount,
                         LastLoginDate = _user.LastLoginDate,
                         LastName = _user.LastName,
                         PhoneNumber = _user.PhoneNumber,
                         ShippingAddress = _user.ShippingAddress,
                         ShippingCity = _user.ShippingCity,
                         ShippingCompany = _user.ShippingCompany,
                         ShippingCountry = _user.ShippingCountry,
                         ShippingFax = _user.ShippingFax,
                         ShippingFirstName = _user.ShippingFirstName,
                         ShippingLastName = _user.ShippingLastName,
                         ShippingPhoneNumber = _user.ShippingPhoneNumber,
                         StateProvince = _user.StateProvince,
                         ZipCode = _user.ZipCode,
                         ShippingZipCode = _user.ShippingZipCode
                     };
                 }
                 return null;
            }
        }
    }
}